import * as express from "express";
import apiSv from "../../services/apiSV";
const { getJWTToken } = require("@coze/api");

let router = express.Router();

const config = {
  client_type: "jwt",
  client_id: "1133532311481",
  coze_www_base: "https://www.coze.cn",
  coze_api_base: "https://api.coze.cn",
  private_key:
    "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCrUl5aJEolnh8U\nEgVXcqRzKlfM8q5waCrLPYbCRLvKJut44ejCOhTPE/Cew+hrSJYxqCcJC+K8kx/i\nyRZWfAJYV5pe3vkWIxC1VI/4wnGFItLk8fYkWgWMyB64y9JBvJNXF1P0w6tPWGcb\nhvaLUMk8NnIUdd7WxSJzS4bKZ54opk3zx9jzeaA6B6vSmsUAN/cOJxWUsEVcB+nH\nSKTdYvunPCk8T9RpTy3oXm46SYE0XnLhgVjNN64XaN56szB8+cJZGacjEvMO9Mpb\nVFwEQLKu8I1oTZc+hYGKOGvxRLaFf9fXJsW4iTZstOjdXoC9i5lYojupwjL0I4OR\nSU8okx1NAgMBAAECggEAC4Nylyjp2WispVhSBul2cc2TrvtUNnZZrI+gTH1QCCrg\nwklJr4aLECDtQnjfWBkTDEKaX6TugkZ6QG8hKC4XU5aM56P2e21fLxXVbcirgQtb\nvvUf2fDkKeznsPr0ULn6m5dpa1acXwMNktpZcU1m6LwD+KoJ5XHEJJlxH+MiIZBy\nH41zcmquzE2J7KaXVlNagGOZ2lqD0amYPOZCVErZAImmaCp18gm9y5/9Osa3RdRD\noPP51wqYN8U8FKgGy4w8znRD/hJRzKjnKqAq9dNJQH2SmVwJ//Jk24S3LvNiIj3v\nMCaGc44xi7zdPGG91tPi9C2QtLhHF8HHCBPBDTxO4QKBgQDVGOsUh0x86BQDBHYu\nLcHpGM13aN2fP2epZovZ6J8uCgSlMXmPCK5hFP9nDnzcBYuKcbgy5GolIhDz96tA\nyJPzQKSQXd5njMzmOBff8xT5PxXg/1rA+pv5efzim3nrQlEB0CosE29GZSLbF6xq\nkSXOly7DROeewZpFq+yzl4mrJQKBgQDN0FPsNPNPl66ERcqc7haayjJA4kYc7XGL\nY1L+JrP08StBNbOuL5HfK/cv7hf4yuBit9gs9fjNqOcV5gLLjnqsqjrIjcZoCMUv\ntSX01Lnua7faBbZT6Xx7VtcJev9pRE2ANguogDGHwhGFxAzTWu6/B7Z+oq4Q/SWD\nJm9a5NTlCQKBgQCMA+xqN5/9Liv/842HOA+CDgbq359GxE1jU/qt+helE3YL6J8b\naTPSnA5Yc398vbP7Ck9iOQy5S1HiSyhdz6zOlvnJ1bONTElRNygQwNJWFqpUhNPn\nmoKjgBC2ADwyZQd+7nzXKwnhMhCd7kceWjKCNHnIWdzqRgEMqDNFqVsOdQKBgAek\nAUmWmaXNysPMV35F9H7gnMb93355biqvkEgxJaMlRObR2I7R40d2uWGFJEyvM29W\nxea9ZvQzngiZFmd6LaG1XCNP6Bq6oJ9pyTPypchtJVEpvyAUXfOyQJ9weoTs7Tei\nSgKCZ9RTZxUhNxytlolu/t5qqa2lyDu7NI0aS7EJAoGBAIT19itOaoGylvb5WKi/\nmCcMG6d4RFOoxL06q+uneEYp5D9sQH1wj3RW8nYHr+vrFeu0km++bvnaRp+wBHwe\n5NgR0Nf8lG1a0RZXEE3tjEl9G6Ecb7sjpUqBy2pEktJaYV1qnpdGgRcxczA4r3F7\nxabrGx0B0+hTTxdO5pl7lE24\n-----END PRIVATE KEY-----",
  public_key_id: "coSlGzsZTfuzbdVTDMiV2UtGUKH-UeF9ZpTGWQ4BYNk",
};
router.post("/AIbotInfo", async (req, res, next) => {
  try {
    let data = await apiSv(req.api.v4.get, {
      action: "corp.config.get",
      companyID: req.companyId,
      sessionKey: req.t,
      mark: "instructorAgent",
    });
    let data2 = await apiSv(req.api.v4.post, {
      action: "course.instructor.get",
      companyID: req.companyId,
      sessionKey: req.t,
      instrId: req.body.instrId,
      userid: req.body.userid,
    });
    return res.json({
      status: true,
      data: {
        AgentConfig: data.data.config,
        Agentbot: data2.data.instrs,
      },
    });
  } catch (e) {
    return next(e);
  }
});
router.get("/AIbotLogin", async (req, res, next) => {
  try {
    const oauthToken = await getJWTToken({
      baseURL: config.coze_api_base,
      appId: config.client_id,
      aud: new URL(config.coze_api_base).host,
      keyid: config.public_key_id,
      privateKey: config.private_key,
    });
    return res.json({ status: true, data: oauthToken });
  } catch (error) {
    console.error("Failed to get JWT OAuth token:", error);
    return res.status(500).json({ status: false, error: error });
  }
});
export default router;
